package com.hikvision.hrms.controller;

import com.hikvision.hrms.model.Admin;
import com.hikvision.hrms.service.AdminService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import java.util.*;

/**
 * @author : yangliu449
 * @date :  2019/1/29
 */
@Controller
public class AdminController {
    @Autowired
    AdminService adminService;

//    统计数据库中的数据
    int total ;
//    显示登陆页面
    @RequestMapping("/")
    @ResponseBody
    public  ModelAndView showAdmin(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login");
        return modelAndView;
    }

    //    显示首页
    @RequestMapping("/indexShow")
    @ResponseBody
    public  ModelAndView showIndex(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/index");
        return modelAndView;
    }
    @RequestMapping("/login")
    @ResponseBody
    public ModelAndView login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
        List<Admin> list= adminService.login(new Admin(username, password));
        ModelAndView modelAndView = new ModelAndView();
        if(list.size()!= 0){
            modelAndView.setViewName("page/index");
            modelAndView.addObject("adminName",username);
            return modelAndView;
        }else {
            modelAndView.setViewName("page/400");
            return modelAndView;
        }


    }

//   显示管理员页面
    @RequestMapping("/adminShow")
    @ResponseBody
    public ModelAndView adminShow(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/admin");
        return modelAndView;
    }


//  数据库中管理员的数据
    @RequestMapping("/adminData")
    @ResponseBody
    public List<Admin> adminData(){
        List<Admin> list = adminService.adminData();
        System.out.println(list.size());
        total = list.size()+1;
        return list;
    }
    @RequestMapping("/addAdmin")
    @ResponseBody
    public void addAdmin(@RequestParam(value = "id")Integer id,@RequestParam(value = "username") String username, @RequestParam(value = "password") String password ,@RequestParam(value = "role_name")String role_name){
       Admin admin = new Admin();
       admin.setId(total);
       admin.setPassword(password);
       admin.setUsername(username);
       admin.setRole_name(role_name);
       adminService.addAdmin(admin);
    }
    @RequestMapping("/updateAdmin")
    @ResponseBody
    public void updateStudent(@RequestParam(value = "id")Integer id,@RequestParam(value = "username") String username, @RequestParam(value = "password") String password ,@RequestParam(value = "role_name")String role_name){
        Admin admin = new Admin();
        admin.setId(id);
        admin.setUsername(username);
        admin.setPassword(password);
        admin.setRole_name(role_name);
        System.out.println(admin);
        adminService.updateAdmin(admin);
    }
    @RequestMapping("/deleteAdmin")
    @ResponseBody
    public void deleteAdmin(@RequestParam(value = "id")Integer id,@RequestParam(value = "username") String username, @RequestParam(value = "password") String password ,@RequestParam(value = "role_name")String role_name){
        adminService.deleteAdmin(id);
    }
}
